﻿using GKQC.DbHelper.Dao;
using GKQC.Language;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GKQC.Forms
{
    public partial class FormSqlQuery : Form
    {
        AllDbContext dbContext;
        public FormSqlQuery(AllDbContext dbContext)
        {
            InitializeComponent();
            this.dbContext = dbContext;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrWhiteSpace(txtSql.Text))
            {
                return;
            }

            string sqlLower = txtSql.Text.ToLower();
            if (sqlLower.Contains("create") || sqlLower.Contains("delete") || sqlLower.Contains("drop")
                || sqlLower.Contains("truncate") || sqlLower.Contains("update") || sqlLower.Contains("drop")
                || sqlLower.Contains(";")
                || sqlLower.Contains("go") 
                || !sqlLower.Contains(" where ")
                )
                return;

            try
            {
                DataTable table = new DataTable();
                if (cboDb.SelectedIndex == 0)
                    table = dbContext.QcDb.Ado.GetDataTable(txtSql.Text);
                else if (cboDb.SelectedIndex == 1)
                    table = dbContext.ErpDb.Ado.GetDataTable(txtSql.Text);

                dataGridView1.DataSource = table;
            }
            catch(Exception ex)
            {
                QMessageBox.Show(ex.Message);
            }
        }
    }
}
